package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonPage;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Counsellor.CounsellorCondition;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCounsellor;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Counsellor.CounsellorAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Counsellor.CounsellorGet;
import cn.yhbk.web.modules.manager.Pojo.VO.Counsellor.CounsellorUpdateForUser;
import cn.yhbk.web.modules.manager.Service.IFlagService;
import cn.yhbk.web.modules.manager.Service.ITbCounsellorService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import cn.yhbk.web.modules.ums.service.UmsRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 辅导员表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@RestController
@Api(tags="辅导员管理")
@RequestMapping("/tbCounsellor")
public class TbCounsellorController {


    @Autowired
    private ITbCounsellorService counsellorService;

    @Autowired
    private UmsRoleService roleService;

    @Autowired
    private IFlagService flagService;

    @Autowired
    private ITbStudentService studentService;

    @ApiOperation("添加辅导员")
    @PostMapping("/manage/")
    public CommonResult addCounsellor(@RequestBody @Valid CounsellorAdd add){

        boolean success = counsellorService.addCounsellor(add);
        if(success)return CommonResult.success("添加成功!");
        return CommonResult.failed("添加失败!");
    }

    @ApiOperation("获取辅导员")
    @GetMapping("/manage")
    public CommonResult getCounsellor(@RequestParam(name="currentPage",defaultValue = "1")Long currentPage,
                                      @RequestParam(name="pageSize",defaultValue = "5")Long pageSize,
                                      CounsellorGet condition){

        //拿到辅导员角色的id
        Long counsellorRoleId = flagService.getCounsellorRoleId();
        List<Long> adminIds = roleService.getAdminIdsByRoleId(counsellorRoleId);
        if(adminIds.isEmpty())adminIds.add(-1L);

        //构造条件dto
        CounsellorCondition counsellorCondition = new CounsellorCondition();
        BeanUtil.copyProperties(condition,counsellorCondition);
        counsellorCondition.setAdminIds(adminIds);
        Page<TbCounsellor> page = counsellorService.getCounsellor(currentPage, pageSize, counsellorCondition);

        return CommonResult.success(CommonPage.restPage(page));
    }


    //修改自己的信息
    @ApiOperation("辅导员修改自己的信息")
    @PutMapping("/user")
    public CommonResult updateCounsellorForUser(@RequestBody CounsellorUpdateForUser updateForUser){
        Long currentAdminId = MyUtil.getCurrentAdminId();
        //构造条件
        TbCounsellor counsellor = new TbCounsellor();
        BeanUtil.copyProperties(updateForUser,counsellor);

        boolean success = counsellorService.updateByAdminId(currentAdminId,counsellor);
        if(success)return CommonResult.success("修改成功!");

        return CommonResult.failed("修改失败!");
    }

    //获取自己得信息
    @ApiOperation("辅导员获取自己得信息")
    @GetMapping("/user/getInfo")
    public CommonResult getCounsellorInfo(){
        Long adminId = MyUtil.getCurrentAdminId();
        TbCounsellor counsellor = counsellorService.getCounsellorByAdminId(adminId);
        return CommonResult.success(counsellor);
    }

    @ApiOperation("管理员修改辅导员信息")
    @PutMapping("/manage/{counsellorId}")
    public CommonResult updateCounsellor(@PathVariable Long counsellorId,@RequestBody CounsellorUpdateForUser updateForUser){

        //构造条件
        TbCounsellor counsellor = new TbCounsellor();
        BeanUtil.copyProperties(updateForUser,counsellor);
        counsellor.setId(counsellorId);
        boolean success = counsellorService.updateById(counsellor);
        if(success)return CommonResult.success("修改成功!");

        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("删除辅导员")
    @DeleteMapping("/manage/{counsellorId}")
    public CommonResult deleteCounsellor(@PathVariable Long counsellorId){
        boolean success = counsellorService.removeCounsellor(counsellorId);

        if(success)return CommonResult.success("删除成功!");

        return CommonResult.failed("删除失败!");
    }

    @ApiOperation("分配辅导员班级")
    @PutMapping("/manage/giveClass/{counsellorId}")
    public CommonResult giveClass(@PathVariable("counsellorId") Long counsellorId,
                                  @RequestBody List<Long> classIds){
        Boolean flag = counsellorService.giveClass(counsellorId, classIds);

        if(flag) return CommonResult.success("分配成功!");

        return CommonResult.failed("分配失败!");
    }


    @ApiOperation("获取辅导员管理的班级")
    @GetMapping("/getMyClass")
    public CommonResult getMyClass(){
        Long counsellorId = MyUtil.getCurrentAdminId();
        TbStudent student = studentService.getOne(new QueryWrapper<TbStudent>().eq("admin_id", counsellorId));
        List<TbClass> classes = counsellorService.getMyClass(student.getId());

        return CommonResult.success(classes);

    }
}
